//  ========== 
//  = 宾和   忘记密码业务JS = 
//  ========== 
var telphone;
var InterValObj; //timer变量，控制时间  
var count = 59; //间隔函数，1秒执行  
var curCount; //当前剩余秒数  
var Main = function() {
	mui.init({
		swipeBack: false //启用右滑关闭功能
	});
	this.init();
	this.even();
	this.resend();
};
Main.prototype = {
	init: function() {
		telphone = Util.requestURL('telphone');
	},
	even: function() {
		$("#btn-sendSms").on('tap', function() {
			if(!Verify.phone(telphone)) {
				Util.toast('操作异常，请重新操作');
				return false;
			}
			$("#btn-sendSms").attr('disabled', 'disabled');
			//发送注册短信验证码
			var sendSmsUrl = server_url + "/codecreate.do";
			var param = {
				isMobile: isMobile,
				notRegCode: 'y',
				sms_code_type: 'check_telphone',
				telphone: telphone,
				randomCode: Math.random(),
				memID: Util.memID()
			}
			$.getJSON(sendSmsUrl, param, function(retData) {
				if(retData.status == 200) {
					mui.toast("短信验证码发送成功");
					curCount = count;
					$("#resendSms").text(curCount + " 秒后可重发");
					InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次 
				} else {
					mui.toast(retData.errMsg);
				}
			});
		});

		function checkPassword(password) {
			if(!password) {
				mui.toast("请输入密码");
				return false;
			}
			if(!(/^([A-Za-z]|[0-9]){6,15}$/.test(password))) {
				mui.toast("请输入6-15位,字母或数字");
				return false;
			}
			return true;
		}
		$('#btn-forgetPassword').on('tap', function() {
			var code = $("#code").val();
			var password = $("#password").val();
			if(!code) {
				mui.toast('请输入验证码');
				return false;
			}
			if(!checkPassword(password)) {
				return false;
			}
			var checkCodeUrl = server_url + "/user/checkCodeBpCustMember.do";
			var param = {
				"isMobile": isMobile,
				"checkCode": code,
				"telphone": telphone
			}
			Util.loading();
			$.getJSON(checkCodeUrl, param, function(retData) {
				Util.closeLoading();
				if(!retData.success) {
					mui.toast(retData.errMsg);
				} else {
					var random = retData.data;
					var forgetPasswordUrl = server_url + "/user/resetPasswordBpCustMember.do";
					var forgetPasswordParam = {
						"isMobile": isMobile,
						"password": password,
						"telphone": telphone,
						"random": random
					}
					Util.loading();
					$.getJSON(forgetPasswordUrl, forgetPasswordParam, function(retData) {
						Util.closeLoading();
						if(!retData.errMsg) {
							//询问框
							layer.open({
								content: '密码修改成功',
								btn: ['确定'],
								yes: function(index) {
									layer.close(index);
									mui.openWindow("login.html?telphone=" + telphone);
								}
							});
						} else {
							mui.toast(retData.errMsg);
						}
					})
				}
			})
		})
	},
	resend: function() {
		$("#btn-sendSms").attr('disabled', 'disabled');
		curCount = count;
		$("#btn-sendSms").text(curCount + " 秒后可重发");
		InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次 
	}
}

//timer处理函数  
function SetRemainTime() {
	if(curCount == 0) {
		$("#btn-sendSms").removeAttr('disabled');
		window.clearInterval(InterValObj); //停止计时器  
		$("#btn-sendSms").text("重新获取");
	} else {
		curCount--;
		$("#btn-sendSms").text(curCount + " 秒后可重发");
	}
}